/* global process */

import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'

// 实现 @ 等于 ./src -- 绝对路径
import path from 'path'

import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'

import { viteMockServe } from 'vite-plugin-mock'

// https://vite.dev/config/
export default defineConfig(({ command, mode }) => {
    const env = loadEnv(mode, process.cwd(), '')

    return {
        plugins: [
            vue(),
            viteMockServe({
                mockPath: 'mock',
                enable: command === 'serve',
            }),
            createSvgIconsPlugin({
                iconDirs: [path.resolve(process.cwd(), 'src/assets/icons')],
                symbolId: 'icon-[dir]-[name]',
            }),
        ],
        // 设置文件 ./src 路径为 @
        resolve: {
            alias: {
                '@': path.resolve('./src'),
            },
        },
        css: {
            preprocessorOptions: {
                scss: {
                    additionalData: ` @use "@/styles/variable.scss" as *; `,
                },
            },
        },
        server: {
            proxy: {
                [env.VITE_APP_BASE_API]: {
                    target: env.VITE_SERVER,
                    changeOrigin: true,
                    rewrite: (path: string) => path.replace(/^\/api/, ''),
                },
            },
        },
    }
})
